package com.socket.nio.nio.chat.example2.server.handler;

import cn.hutool.core.collection.CollectionUtil;
import com.socket.nio.nio.chat.example2.message.GroupMembersRequestMessage;
import com.socket.nio.nio.chat.example2.message.GroupMembersResponseMessage;
import com.socket.nio.nio.chat.example2.server.session.GroupSessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Set;

/**
 * @author Hexudong
 * @date 2025/5/18
 */
@ChannelHandler.Sharable
public class GroupMembersRequestMessageHandler extends SimpleChannelInboundHandler<GroupMembersRequestMessage> {
  @Override
  protected void channelRead0(ChannelHandlerContext ctx, GroupMembersRequestMessage message) throws Exception {
    Set<String> members = GroupSessionFactory.getGroupSession().getMembers(message.getGroupName());
    if (CollectionUtil.isNotEmpty(members)) {
      ctx.writeAndFlush(GroupMembersResponseMessage.builder()
          .members(members)
          .build());
    }
  }
}
